<?php

require_once ('application/controllers/basecontroller.php');
require_once ("application/controllers/data/userInfo.php");
class user extends BaseController {
	
	function __construct() {
		parent::__construct ();
		$this->load->model('usermodel');
		$this->session->set_userdata("navindex",2);
	}

	function _loadContentView()
	{   
		if (isset($this->baseControllerData[$this->DATATYPE_ERROR]))
		{
			parent::_loadContentView();
			unset($this->baseControllerData[$this->DATATYPE_ERROR]);
		}else if (!isset($this->baseControllerData["type"]) && $this->session->userdata("username")==null)
		{
			$this->load->view('loginview');
			
		}else if($this->baseControllerData["type"]==0)
		{
            $this->load->view("user/userinfoview",$this->baseControllerData[$this->DATATYPE_CONTENT]);
		}
		else if ($this->baseControllerData["type"]==1)
		{
			$this->_addUserInfo();
		}else if($this->baseControllerData["type"]==2)
		{
			$this->load->view("user/userinfodetail",$this->baseControllerData[$this->DATATYPE_CONTENT]);
		}
		else if($this->baseControllerData["type"]==3)
		{
			$this->load->view("user/edituserinfo",$this->baseControllerData[$this->DATATYPE_CONTENT]);
		}
		else if ($this->baseControllerData["type"]==4)
		{
			$this->load->view("user/userinfosearch",$this->baseControllerData[$this->DATATYPE_CONTENT]);
		}	
		else if ($this->baseControllerData["type"]==5)
		{
			$this->load->view("user/sendmail",$this->baseControllerData[$this->DATATYPE_CONTENT]);
		}	
		else if ($this->baseControllerData["type"]==6)
		{
			$this->load->view("user/sendmailnext",$this->baseControllerData[$this->DATATYPE_CONTENT]);
		}
		
	}
	
	function _loadAsideView()
	{   
	   if(!isset($this->baseControllerData["type"]) && $this->session->userdata("username")==null)
	   {
		   $this->load->view("useraside");
		}else{
          
		  $this->load->view("user/userinfoaside");
		}
	}
	function logintest()
	{
		$userinfotmp = $this->usermodel->testuser($_POST["username"],$_POST["passwd"]);;
		
		if(isset($userinfotmp))
		{
			$this->session->set_userdata("userid",$userinfotmp->id);
			$this->session->set_userdata("username",$userinfotmp->username);
			$this->session->set_userdata("passwd",$userinfotmp->password);
			$this->session->set_userdata("usertype",$userinfotmp->type);
			redirect(base_url("index.php/home"));
		}
		else
		{
			$this->showerror("login failure");
		}

		//redirect(base_url("index.php/user/"),"post",400);
		
		
	}
	function logout()
	{
		$this->session->unset_userdata("username");
		$this->session->unset_userdata("passwd");
		$this->session->unset_userdata("usertype");
		$this->session->unset_userdata("userid");
		redirect(base_url("index.php/user/"));
	}
	function _loadTrayView()
	{
		if ($this->session->userdata("username"))
		{
			$userinfotmp = $this->session->userdata("username");
			$data["username"] = $userinfotmp;
		}
		else
		{
			$data["username"] = null;
		}
		$this->load->view("app/trayview",$data);
	}
	function adduserinfo()
	{
		if ($this->session->userdata("usertype")== 4)
		{
			$this->baseControllerData["type"]=1;
			parent::index();
		}
		else
		{
			redirect(base_url("index.php/user/"));
		}
				
	}
	function _addUserInfo()
	{
		$this->load->view("user/adduser");
	}
	function addtomodel()
	{
		if($this->session->userdata("username")==null)
		{
			redirect(base_url("index.php/user/"));
			return ;
		}
		$data = $this->usermodel->checkuser($_POST["username"]);
		if($_POST["username"]=="" || $data) {
		 $this->showerror("用户名已存在或不能为空");
		}else{
		$num = $this->usermodel->saveuserinfo($_POST["username"],$_POST["password"]
			,$_POST["grade"],$_POST["person"],$_POST["post"]
			,$_POST["age"],$_POST["phone"],$_POST["email"],$_FILES["pic"]);
			
		$this->userlist(0);
		}
	}	
	function edit($id)
	{
		if ($this->session->userdata("usertype")== 4)
		{
			$this->baseControllerData["type"]=3;
			$data = $this->usermodel->getuserinfodetail($id);
	
			$this->baseControllerData[$this->DATATYPE_CONTENT] = $data[0];
			parent::index();
		}
			else
		{
			$this->showerror("please login as admin");
		}
	}
	function userlist($start=0)
	{
		$size =10;
		if ($this->session->userdata("username")!= null)
		{
			$this->__listUserInfo(intval($start),$size);
		}
		else
		{
			redirect(base_url("index.php/user/"));
		}
	}
	function __listUserInfo($start,$size)
	{
		
		$num = $this->usermodel->getuserinfo();		
		$list =$this->usermodel->getuserlist($start,$size);
		$data["list"] = $list;
		$data["start"] = $start;
		$data["num"] = $num;
		$data["size"] = $list->num_rows();
		$data["type"] = 0;
		$this->baseControllerData["type"] = 0;
		$this->baseControllerData[$this->DATATYPE_CONTENT]=$data;
		//$this->load->view("case/caseinfoview",$data);
		parent::index($this->baseControllerData);
	}
	function updatetomodel()
	{
		if($this->session->userdata("username")==null)
		{
			redirect(base_url("index.php/user/"));
			return ;
		}
		$num = $this->usermodel->updateuserinfo($_POST["id"],$_POST["password"],$_POST["grade"]
			,$_POST["person"],$_POST["post"],$_POST["age"]
			,$_POST["phone"],$_POST["email"],$_FILES["pic"]);
			
		$this->userlist(0);

	}
	function delete($id)
	{
		if ($this->session->userdata("usertype")== 4)
		{
			$this->baseControllerData["type"]=0;
			$data = $this->usermodel->delete($id);
			$this->userlist(0);
		}
		else
		{
			$this->showerror("please login as admin");
		}
	}
	function detail($id)
	{
		if ($this->session->userdata("username")!= null)
		{
			$this->baseControllerData["type"]=2;
			$data = $this->usermodel->getuserinfodetail($id);
	        
			 $this->baseControllerData[$this->DATATYPE_CONTENT] = $data[0];
			 parent::index();
			
		}
	}
	function send_notics($email="")
	{
	//die("aa");
		if ($this->session->userdata("usertype")== 4)
		{
			$this->baseControllerData["type"]=5;
			$data["email"]=$email;
			$this->baseControllerData[$this->DATATYPE_CONTENT]=$data;
			parent::index();
		}
		else
		{
			$this->showerror("please login as admin");
		}
		
	}
	function sendtonext($email="")
	{
	//die("aa");
		if ($this->session->userdata("usertype")== 4)
		{
			$this->baseControllerData["type"]=6;
			$list=$this->usermodel->getuserall();
			//die(print_r($list->result_array()));
			$i=1;
			foreach($list->result_array() as $row){
              $tmp_list[$row['person']][$i]=$row;
			  $i++;
			  }
			
			//die(print_r($tmp_list));
			$data["email"]=$email;
			$data["title"]=$_POST["title"];
			$data["message"]=$_POST["message"];
			$data["list"]= $tmp_list;
			$this->baseControllerData[$this->DATATYPE_CONTENT]=$data;
			parent::index();
		}
		else
		{
			$this->showerror("please login as admin");
		}
		
	}
	
	function sendtomodel()
	{
		if($this->session->userdata("username")==null)
		{
			redirect(base_url("index.php/user/"));
			return ;
		}
		$email=array_filter($_POST["uid"]);		
		//die(print_r($email));
		if($num = $this->usermodel->sendmail($email,$_POST["title"],$_POST["message"])){			
		  $this->showerror("发送成功");
		}else{
           $this->showerror("发送失败");
        }
	}
}

?>